<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    body {
        width: 100%;
        height: 500px;
        background-image: url(../images/bg.jpg);
        background-repeat: no-repeat;
        background-size: cover;
    }

    .all {
        width: 250px;
        background-color: rgba(252, 248, 248, 0.5);
        margin: 200px 600px;
        padding: 10px;
    }

    .all h3 {
        margin: 20px 80px;
        align-items: center;
    }

    .all input {
        width: 200px;
        height: 20px;
        margin: 10px 25px;
        border: 0px;
        border-bottom: 1px solid rgba(9, 9, 9, 0.4);
        margin-bottom: 10px;
        background-color: rgba(252, 248, 248, 0.5);
        /* 去掉输入时的边框 */
        outline: none;
    }

    .all button {
        margin: 25px;
        width: 200px;
        height: 30px;
        line-height: 30px;
        font-weight: 700;
        font-size: 15px;
        background-color: #176bb0;
        color: #fff;
        border: 0;
        border-radius: 30px;
    }
    .all .cancel{
        margin-top: 0;
    }
</style>

<body>
    <div class="body">
        <div class="all">
            <h3>修改密码</h3>
            <input type="text" placeholder="请输入旧密码"></input>
            <input type="text" placeholder="请输入新密码" id="password"></input>
            <button onclick="set()">确认修改</button>
            <button class="cancel" onclick="cancel()">取消修改</button>
        </div>
    </div>
</body>
<script>
    var username = window.sessionStorage.getItem('username');
    console.log(username);
    function set() {
        var newpassword = document.getElementById("password").value;
        console.log(newpassword);
        console.log(typeof (newpassword));
        var httpRequest = new XMLHttpRequest(); //第一步：建立所需的对象
        httpRequest.open('POST', 'http://192.168.0.108:5000/reset', true);
        httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        httpRequest.send("username=" + username + "&password=" + newpassword); //第二步：打开连接  将请求参数写在url中  ps:"./Ptest.php?name=test&nameone=testone"
        httpRequest.onreadystatechange = () => {
            if (httpRequest.readyState == 4 && httpRequest.status == 200) {
                var data = JSON.parse(httpRequest.responseText);
                if (data == 1) {
                    window.sessionStorage.setItem('password', newpassword);
                    location.replace("index.html");
                }
            }
        }
    }
    // 取消修改
    function cancel(){
        location.replace("user.html");
    }
</script>

</html>